package cn.zmit.kuxi.activity;

import java.util.HashMap;
import java.util.Map;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.common.utils.ScreenUtils;
import com.xdroid.common.utils.StringUtils;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.Response;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.request.RequestTask;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

/** * @author @parameter * @return */
public class PasswordSet extends BaseActivity {
	@ViewInject(R.id.edt_old_pwd)
	private EditText old_pwd;
	@ViewInject(R.id.edt_new_pwd)
	private EditText new_pwd;
	@ViewInject(R.id.edt_again_pwd)
	private EditText again_pwd;
	private String oldPwd;
	private String newPwd;
	private String againPwd,userid;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//
		setContentView(R.layout.activity_password_set);
		ViewUtils.inject(this);
		init();
		initTitleBar();
		old_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
		new_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
		again_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
	}

	private void initTitleBar() {
		setTitleView("密码设置");
		
		final TextView editTextView = new TextView(context);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) ScreenUtils.dpToPx(context, 40),
				RelativeLayout.LayoutParams.MATCH_PARENT);
		editTextView.setLayoutParams(params);
		editTextView.setGravity(Gravity.CENTER);
		editTextView.setTextColor(getResources().getColor(R.color.black));
		editTextView.setTextSize(14);
		editTextView.setText("完成");
		com.xdroid.common.utils.ViewUtils.setPadding(editTextView, 0, 0, 20, 0);
		editTextView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkForm()) {
					Map<String, Object>map=new HashMap<String,Object>();
					map.put("user_id", userid);
					map.put("new_password", newPwd);
					map.put("old_password", oldPwd);
					getSharedPreferences("saveUserNamePwd", MODE_PRIVATE).edit().putString("password",newPwd).commit();
					RequestTask.getInstance().doResetPassword(PasswordSet.this, map, new onRequestListener());
				}
			}
		});
		getTitleBar().addRightView(editTextView);
	}
private class onRequestListener extends OnRequestListenerAdapter<Object>{
	@Override
	public void onDone(XDroidRequest<?> request, String response, DataType dataType) {
		// TODO Auto-generated method stub
		super.onDone(request, response, dataType);
		JsonData jsonData=JsonData.create(response);
		if (jsonData.optString(Response.CODE).equals("1")) {
			
			ToastUtils.show(context, "更新成功");
			
			finish();
		}
		if (jsonData.optString(Response.CODE).equals("0")) {
			ToastUtils.show(context, "原密码不正确");
		}
	}
	
}
	private void init() {
		oldPwd = old_pwd.getText().toString().trim();
		newPwd = new_pwd.getText().toString().trim();
		againPwd = again_pwd.getText().toString().trim();
		if (PreferenceHelper.readString(context, SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID)!=null) {
			 userid=PreferenceHelper.readString(context, SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID);
		}
		
	}
	 

	// 表单验证
	private Boolean checkForm() {
		oldPwd = old_pwd.getText().toString().trim();
		newPwd = new_pwd.getText().toString().trim();
		againPwd = again_pwd.getText().toString().trim();
		if (StringUtils.isEmpty(oldPwd)) {
			ToastUtils.show(context, "请输入旧密码");
			return false;
		} else if (StringUtils.isEmpty(newPwd)) {
			ToastUtils.show(context, "请输入新密码");
			return false;
		} else if (StringUtils.isEmpty(againPwd)) {
			ToastUtils.show(context, "请再次输入密码");
			return false;
		} else if (oldPwd.length() < 6) {
			ToastUtils.show(context, "密码长度不符合要求");
			return false;
		} else if (newPwd.length() < 6) {
			ToastUtils.show(context, "密码长度不符合要求");
			return false;
		} else if (againPwd.length() < 6 ) {
			ToastUtils.show(context, "密码长度不符合要求");
			return false;
		}else if( oldPwd.length() > 16){
			ToastUtils.show(context, "密码长度不符合要求");
			return false;
		}else if( newPwd.length() > 16){
			ToastUtils.show(context, "密码长度不符合要求");
			return false;
		}else if( againPwd.length() > 16){
			ToastUtils.show(context, "密码长度不符合要求");
			return false;
		}
		
		return true;
	}
}
